package adminServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swim2.sessionbeans.AdminSystemRemote;
import swim2.entitybeans.*;

public class AlterAbilitiesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
    
    public AlterAbilitiesServlet() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{	
			Context ctx = getInitialContext();			
			AdminSystemRemote adminSystemRemote = (AdminSystemRemote) ctx.lookup("adminJNDI");
			int adminId = Integer.parseInt(request.getParameter("adminId"));
			String mail = request.getParameter("mail");
			String password = request.getParameter("password");
			ArrayList<Ability> allAbilities = (ArrayList<Ability>) adminSystemRemote.getAllAbilities();
			
			request.setAttribute("adminId", adminId);
			request.setAttribute("mail", mail);
			request.setAttribute("password", password);
			request.setAttribute("allAbilities", allAbilities);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/AdminJsp/AlterAbilities.jsp");
			dispatcher.forward(request, response);
		}
		catch (Exception e) {
			e.printStackTrace();
			
		}
		
		
		

	}

	static public Context getInitialContext() throws Exception{
		
		Hashtable<String,String> env = new Hashtable<String,String>();
		
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL, "localhost:1099");
	
		return new InitialContext(env);
		
	}
	
}
